package com.example.mountainwalker.service.impl;

import com.example.mountainwalker.entity.UserInfo;
import com.example.mountainwalker.mapper.UserMapper;
import com.example.mountainwalker.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserInfo> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public List<UserInfo> getUserByKeyword(String keyword) {
        if (keyword != null && keyword.matches("^\\d+$")) {
            // 纯数字，查 user_id
            UserInfo user = userMapper.getUserById(Integer.parseInt(keyword));
            if (user != null) {
                return Collections.singletonList(user);
            } else {
                return Collections.emptyList();
            }
        } else {
            // 其它，模糊查
            return userMapper.getUserByKeyword(keyword);
        }
    }

    @Override
    public UserInfo getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public int deleteUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public int addUser(UserInfo userInfo) {
        return userMapper.addUser(userInfo);
    }

    @Override
    public int updateUser(UserInfo userInfo) {
        return userMapper.updateUser(userInfo);
    }
}